package com.dk.shared.autoconfigure.security;

import com.dk.shared.autoconfigure.common.SharedProperties;
import com.dk.shared.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
public class SecurityAutoConfiguration implements WebMvcConfigurer {

    private final String[] defaultExcludePaths = {
            "/rest/v1/auth/**"
    };

    private List<String> excludePaths = new ArrayList<>(Arrays.asList(defaultExcludePaths));

    @Autowired
    public SharedProperties sharedProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if(!CollectionUtils.isEmpty(sharedProperties.getSecurityPermits())){
            excludePaths.addAll(sharedProperties.getSecurityPermits());
        }
        registry.addInterceptor(authInterceptor())
                .addPathPatterns("/rest/**")
                .excludePathPatterns(excludePaths);
    }


    @Bean
    public AuthInterceptor authInterceptor() {
        return new AuthInterceptor();
    }

}
